﻿using NLog;

namespace ThresholdAssistant.NLogSdk
{
    /// <summary>
    /// 日志帮助类
    /// </summary>
    public static class NLogUtil
    {
        /// <summary>
        /// 日志记录器
        /// </summary>
        private static readonly ILogger _log = LogManager.GetLogger(NLogConst.GlobalLoggerName);

        /// <summary>
        /// 跟踪日志
        /// </summary>
        /// <param name="model"></param>
        public static void Trace(NLogModel model) => SaveLog(LogLevel.Trace, model);

        /// <summary>
        /// 调试日志
        /// </summary>
        /// <param name="model"></param>
        public static void Debug(NLogModel model) => SaveLog(LogLevel.Debug, model);

        /// <summary>
        /// 消息日志
        /// </summary>
        /// <param name="model"></param>
        public static void Info(NLogModel model) => SaveLog(LogLevel.Info, model);

        /// <summary>
        /// 写入警告日志
        /// </summary>
        /// <param name="model">日志模型</param>
        public static void Warn(NLogModel model) => SaveLog(LogLevel.Warn, model);

        /// <summary>
        /// 写入异常日志
        /// </summary>
        /// <param name="model">日志模型</param>
        public static void Error(NLogModel model) => SaveLog(LogLevel.Error, model);

        /// <summary>
        /// 写入致命性错误
        /// </summary>
        /// <param name="model">日志模型</param>
        public static void Fatal(NLogModel model) => SaveLog(LogLevel.Fatal, model);

        /// <summary>
        /// 主要用来跟踪SQL语句，线上最好不要开启此类型的日志
        /// </summary>
        /// <param name="content">文本内容</param>
        /// <param name="serialNo">操作编号</param>
        public static void Trace(string content, string serialNo = "")
        {
            var logInfo = LogEventInfo.Create(LogLevel.Trace, _log.Name, content);
            logInfo.Properties[NLogConst.UniqueId] = serialNo;
            logInfo.Properties[NLogConst.SerialNo] = serialNo;
            logInfo.Properties[NLogConst.LoggerKeyContentLength] = content?.Length;
            _log.Log(logInfo);
        }

        /// <summary>
        /// 调试日志
        /// </summary>
        /// <param name="content">文本内容</param>
        /// <param name="serialNo">操作编号</param>
        public static void Debug(string content, string serialNo = "")
        {
            var logInfo = LogEventInfo.Create(LogLevel.Debug, _log.Name, content);
            logInfo.Properties[NLogConst.UniqueId] = serialNo;
            logInfo.Properties[NLogConst.SerialNo] = serialNo;
            _log.Log(logInfo);
        }

        /// <summary>
        /// 消息日志
        /// </summary>
        /// <param name="content">文本内容</param>
        /// <param name="serialNo">操作编号</param>
        /// <param name="url"></param>
        public static void Info(string content, string serialNo = "", string url = null)
        {
            var logInfo = LogEventInfo.Create(LogLevel.Info, _log.Name, content);
            logInfo.Properties[NLogConst.LoggerKeyUrl] = url;
            logInfo.Properties[NLogConst.UniqueId] = serialNo;
            logInfo.Properties[NLogConst.SerialNo] = serialNo;
            _log.Log(logInfo);
        }

        /// <summary>
        /// 警告日志
        /// </summary>
        /// <param name="content">文本内容</param>
        /// <param name="serialNo">操作编号</param>
        public static void Warn(string content, string serialNo = "")
        {
            var logInfo = LogEventInfo.Create(LogLevel.Warn, _log.Name, content);
            logInfo.Properties[NLogConst.UniqueId] = serialNo;
            logInfo.Properties[NLogConst.SerialNo] = serialNo;
            _log.Log(logInfo);
        }

        /// <summary>
        /// 写入异常日志
        /// </summary>
        /// <param name="content">文本内容</param>
        /// <param name="serialNo">操作编号</param>
        public static void Error(string content, string serialNo = "")
        {
            var logInfo = LogEventInfo.Create(LogLevel.Error, _log.Name, content);
            logInfo.Properties[NLogConst.UniqueId] = serialNo;
            logInfo.Properties[NLogConst.SerialNo] = serialNo;
            _log.Log(logInfo);
        }

        /// <summary>
        /// 写入致命性错误
        /// </summary>
        /// <param name="content">文本内容</param>
        /// <param name="serialNo">操作编号</param>
        public static void Fatal(string content, string serialNo = "")
        {
            var logInfo = LogEventInfo.Create(LogLevel.Fatal, _log.Name, content);
            logInfo.Properties[NLogConst.UniqueId] = serialNo;
            logInfo.Properties[NLogConst.SerialNo] = serialNo;
            _log.Log(logInfo);
        }

        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="level">日志等级</param>
        /// <param name="model"></param>
        public static void SaveLog(LogLevel level, NLogModel model)
        {
            var logInfo = LogEventInfo.Create(level, _log.Name, model.Content);
            logInfo.Properties[NLogConst.SerialNo] = model.SerialNo;
            logInfo.Properties[NLogConst.Headers] = model.Headers;
            logInfo.Properties[NLogConst.Request] = model.Request;
            logInfo.Properties[NLogConst.Response] = model.Response;
            //logInfo.Properties[NLogConst.RobotSerialNo] = model.RobotSerialNo;
            logInfo.Properties[NLogConst.LoggerKeyUrl] = model.Url;
            logInfo.Properties[NLogConst.LoggerKeyType] = model.Type;
            //logInfo.Properties[NLogConst.MerchantNo] = model.MerchantNo;
            logInfo.Properties[NLogConst.UniqueId] = model.ReqId;
            logInfo.Properties[NLogConst.LoggerKeyTotalSeconds] = model.TotalSeconds;
            logInfo.Properties[NLogConst.LoggerKeyContentLength] = model.Content?.Length;
            _log.Log(logInfo);
        }
    }
}